package com.rc.activity;

import java.util.Calendar;

import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.rc.activity.dao.tbl_Profile;
import com.rc.activity.dao.tbl_ProfileDao;
import com.rc.activity.dao.tbl_UserDao;
import com.rc.activity.provider.Contract;
import com.rc.db.DatabaseSupport;
import com.rc.util.Util;
import com.telpoo.frame.ui.BaseActivity;

public class YourProfile extends BaseActivity implements OnClickListener {

	private EditText txtFirstName, txtLastName, txtPrimaryEmail,
			txtMobilePhone, txtAddress, txtCompany;
	private TextView txtDateofbirth;
	private RadioButton rbMale, rbFemale, rbSingle, rbMarried;
	private LinearLayout llSave, llCancel;
	private tbl_Profile objProfile;

	public static void callIntent(Context ct) {
		Intent it = new Intent(ct, YourProfile.class);
		ct.startActivity(it);
	}

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main_your_profile);

		// register
		txtFirstName = (EditText) findViewById(R.id.txtItemName);
		txtLastName = (EditText) findViewById(R.id.txtLastName);
		txtDateofbirth = (TextView) findViewById(R.id.txtDateofbirth);
		txtPrimaryEmail = (EditText) findViewById(R.id.txtPrimaryEmail);
		txtMobilePhone = (EditText) findViewById(R.id.txtMobilePhone);
		txtAddress = (EditText) findViewById(R.id.txtAddress);
		txtCompany = (EditText) findViewById(R.id.txtCompany);
		rbMale = (RadioButton) findViewById(R.id.rbMale);
		rbSingle = (RadioButton) findViewById(R.id.rbsingle);
		rbMarried = (RadioButton) findViewById(R.id.rbMarried);
		rbFemale = (RadioButton) findViewById(R.id.rbFemale);
		llCancel = (LinearLayout) findViewById(R.id.llCancel);
		llSave = (LinearLayout) findViewById(R.id.llSave);

		// function
		llCancel.setOnClickListener(this);
		llSave.setOnClickListener(this);
		txtDateofbirth.setOnClickListener(this);

		// set default date for TEXTVIEW date of birth
		getDefaultInfor();

	}

	private void getDefaultInfor() {
		objProfile = new tbl_Profile();
		objProfile = DatabaseSupport.getTbl_Profile(YourProfile.this);
		if (objProfile == null) {
			return;
		}
		txtFirstName.setText(objProfile.getFirstName());
		txtLastName.setText(objProfile.getLastName());
		if (!objProfile.getBirthdate().equals("null")) {
			txtDateofbirth.setText(objProfile.getBirthdate());
		}

		rbSingle.setChecked(objProfile.getMaritalStatus().equals("0"));
		rbMarried.setChecked(objProfile.getMaritalStatus().equals("1"));
		txtMobilePhone.setText(objProfile.getMobile());
		txtAddress.setText(objProfile.getAddressFirst());
		txtCompany.setText(objProfile.getCompanyName());
		if (objProfile.getGender())
			rbMale.setChecked(true);
		else
			rbFemale.setChecked(true);
		Cursor cursor = this.getContentResolver().query(
				Contract.CONTENT_URI_USER, null, null, null, null);
		while (cursor.moveToNext()) {
			txtPrimaryEmail.setText(cursor.getString(cursor
					.getColumnIndex(tbl_UserDao.Properties.Email.columnName)));
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.llCancel:
			finish();
			break;
		case R.id.llSave:
			if (validate()) {
				if (this.getContentResolver().update(
						Contract.CONTENT_URI_PROFILE,
						getDataUI().getContentValues(),
						tbl_ProfileDao.Properties.UserID.columnName + " = "
								+ objProfile.getUserID(), null) >= 0) {
					finish();
				}

			}
			break;
		case R.id.txtDateofbirth:
			showDatePickerDialog();
			break;
		default:
			break;
		}
	}

	private tbl_Profile getDataUI() {
		objProfile.setAddressFirst(txtAddress.getText().toString());

		if (!txtDateofbirth.getText().toString()
				.equals(getString(R.string.dateofbirth))) {
			objProfile.setBirthdate(txtDateofbirth.getText().toString());
		}

		objProfile.setCompanyName(txtCompany.getText().toString());
		objProfile.setFirstName(txtFirstName.getText().toString());
		boolean gender = false;
		gender = rbMale.isChecked() ? true : false;
		objProfile.setGender(gender);
		objProfile.setIsUpdate("1");
		objProfile.setLastName(txtLastName.getText().toString());
		objProfile.setMaritalStatus(rbSingle.isChecked() ? "0" : "1");
		objProfile.setMobile(txtMobilePhone.getText().toString());
		return objProfile;
	}

	/**
	 * validate UI
	 */
	private boolean validate() {
		if (!Util.validateText(txtFirstName)) {
			Util.setError(txtFirstName, getString(R.string.input_firstname));
		} else if (txtFirstName.getText().toString().length() > 45) {
			Util.setError(txtFirstName, getString(R.string.invalid_firstname));
		} else if (!Util.validateText(txtLastName)) {
			Util.setError(txtLastName, getString(R.string.input_lastname));
		} else if (txtLastName.getText().toString().length() > 45) {
			Util.setError(txtLastName, getString(R.string.invalid_lastname));
		} else {
			return true;
		}
		return false;
	}

	@SuppressWarnings("deprecation")
	public void showDatePickerDialog() {
		int date = 0, month = 0, year = 0;

		OnDateSetListener callback = new OnDateSetListener() {
			public void onDateSet(DatePicker view, int year, int monthOfYear,
					int dayOfMonth) {
				// Date
				txtDateofbirth.setText(Util.validateDateMonth(dayOfMonth) + "-"
						+ Util.validateDateMonth(monthOfYear + 1) + "-" + year);
			}
		};
		String s = txtDateofbirth.getText() + "";
		if (getString(R.string.dateofbirth).equals(s)) {
			Calendar cal = Calendar.getInstance();
			date = cal.getTime().getDate();
			month = cal.getTime().getMonth();
			year = cal.get(Calendar.YEAR);
		} else {
			String strArrtmp[] = s.split("-");
			date = Integer.parseInt(strArrtmp[0]);
			month = Integer.parseInt(strArrtmp[1]) - 1;
			year = Integer.parseInt(strArrtmp[2]);
		}
		DatePickerDialog pic = new DatePickerDialog(YourProfile.this, callback,
				year, month, date);
		pic.setTitle(getString(R.string.choose_calendar));
		pic.show();
	}

}
